using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerCounterAttackState : PlayerState
{
    private bool canCreateClone;
    public PlayerCounterAttackState(Player player, StateMachine stateMachine, string animName) : base(player, stateMachine, animName)
    {
    }

    public override void Enter()
    {
        base.Enter();
        canCreateClone = true;
        stateTimer = player.counterAttackDuration;
        player.anim.SetBool("SuccessfulCounterAttack", false);
    }

    public override void Exit()
    {
        base.Exit();
    }

    public override void Update()
    {
        base.Update();
        player.SetZeroVelocity();
        Collider2D[] colliders = Physics2D.OverlapCircleAll(player.attackCheck.position, player.attackRadius);
        foreach (var collider in colliders)
        {
            Enemy enemy = collider.GetComponent<Enemy>();
            if (enemy != null && enemy.CanBeStunned())
            {
                player.anim.SetBool("SuccessfulCounterAttack", true);
                AudioManager.Instance.PlaySound((int)E_SoundType.PlayerCounterAttack);
                if (canCreateClone)
                {
                    canCreateClone = false;
                    player.skill.clone.CreateCloneOnCounterAttack(enemy.transform, new Vector3(2 * player.facingDir, 0));
                }
                
            }
        }

        if(stateTimer< 0 || triggerCalled)
            stateMachine.ChangeState(player.idleState);
    }
}
